﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class Soil : UserControl
    {
        public int intIndex;
        public int DeviceSelectNo;//设备选择序号
        public ParameterConfig mf;

        public Soil(ParameterConfig mfs)
        {
            InitializeComponent();
            mf = mfs;
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="type">设备类型</param>
        /// <param name="index">设备标识</param>
        /// <param name="intDeviceSelectNo">设备选择序号</param>
        public void InitValue(int type, int index, int intDeviceSelectNo)
        {
            gc.Text = Common.SoilList[intDeviceSelectNo].ToString() + (index + 1).ToString();
            DeviceSelectNo = intDeviceSelectNo;
            intIndex = index;
            this.Tag = type;
            this.TabIndex = (type + 1) * 10 + index;
        }

        /// <summary>
        /// 设置土壤温湿度传感器配置
        /// </summary>
        public void SetValue()
        {
            nudAddr.Value = ParameterConfig.rtuConfigInfo.SoilTempHumi_Addr[intIndex];
        }

        /// <summary>
        /// 获取土壤温湿度传感器配置
        /// </summary>
        public void GetValue()
        {
            ParameterConfig.rtuConfigInfo.SoilTempHumi_Enable |= Convert.ToByte(0x0001 << intIndex);
            ParameterConfig.rtuConfigInfo.SoilTempHumi_Addr[intIndex] = Convert.ToByte(nudAddr.Value);
            ParameterConfig.rtuConfigInfo.SoilTempHumi_Select[intIndex] = Convert.ToByte(DeviceSelectNo);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除设备?", "删除设备", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Control c = this.Parent;
                c.Controls.Remove(this);
                mf.InitSoilNo();
            }
        }

        private void btnDebug_Click(object sender, EventArgs e)
        {
            MessageInfo.SendMessage(ParameterConfig.mainFormName, ParameterConfig.debugMsg, 0x0B, intIndex + 1);
        }
    }
}
